{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray 对象属性操作详解",
   "id": "80db87b7ca59ebe"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 1. Numpy的内部基本数据类型\n",
    "- 布尔型 --> bool_\n",
    "- 有符号整数型 --> int8(-128~127) / int16 / int32 / int64\n",
    "- 无符号整数型 --> uint8(0~255) / uint16 / uint32 / uint64\n",
    "- 浮点型  -->float16 / float32 / float64\n",
    "- 复数型 --> complex64 / complex128\n",
    "- 字符串型 --> str_，每个字符用32位Unicode编码表示 >U32\n",
    "- 日期类型 --> datetime64\n",
    "\n",
    "## 2. Numpy的类型编码\n",
    "- np.bool_\t           -->     ?\n",
    "-  np.int8/16/32/64 \t-->    i1 / i2 / i4 / i8\n",
    "-  np.uint8/16/32/64\t-->    u1 / u2 / u4 / u8\n",
    "-  np.float/16/32/64\t-->    f2 / f4 / f8\n",
    "-  np.complex 64/128\t-->    c8  / c16\n",
    "-  np.str_\t           -->     U\n",
    "-  np.datetime64\t  -->      M8[Y] M8[M] M8[D] M8[h] M8[m] M8[s]\n",
    "\n"
   ],
   "id": "1d9f985b7a2a32e"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. 自定义数据类型",
   "id": "6fb522e3f4854a47"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T01:19:21.623174Z",
     "start_time": "2025-08-26T01:19:21.610068Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 自定义列表\n",
    "data = [\n",
    "    ('聂茹凤',[90,95,97],19),\n",
    "    ('谭鑫宇',[89,99,69],18),\n",
    "    ('韩耀祖',[77,100,89],16)\n",
    "]\n",
    "\n",
    "# 自定义数据类型\n",
    "# 方式一\n",
    "# U3: 长度为3的Unicode字符串\n",
    "# 3i4/3int32 ： 3个int类型的数字\n",
    "# i4/int32  : 1个int类型的数字\n",
    "# a = np.array(data,dtype='U3,3i4,i4')\n",
    "a = np.array(data,dtype='U3,3int32,int32') # 这里字符串不支持 str_的写法，int i4 或者 int32 的写法都可以\n",
    "\n",
    "print(a)\n",
    "print(a[0])\n",
    "print(a[0]['f0']) # 第一个元素的第一个域 (f0)\n",
    "print(a[0]['f1']) # 第一个元素的第二个域\n",
    "print(a.dtype)\n",
    "\n",
    "print(\"=\" * 30)\n",
    "\n",
    "# 方式二\n",
    "# b = np.array(data,dtype=[('name','str_',3),('score','int32',3),('age','int32')])\n",
    "# b = np.array(data,dtype=[('name','U',3),('score','i4',3),('age','i4')])\n",
    "b = np.array(data,dtype=[('name','U3'),('score','3i4'),('age','i4')])\n",
    "print(b)\n",
    "print(b.dtype)\n",
    "print(b[0])\n",
    "print(b[0]['name'])\n",
    "print(b[0]['score'])\n",
    "\n",
    "print('=' * 30)\n",
    "# 方式三\n",
    "# c = np.array(data,dtype={'names':('name','score','age'),'formats':('U3','3i4','i4')})\n",
    "c = np.array(data,dtype={'names':('name','score','age'),'formats':('U3','3int32','int32')})\n",
    "print(c)\n",
    "print(c.dtype)\n",
    "print(c[0])\n",
    "print(c[0]['name'])\n",
    "print(c[0]['score'])"
   ],
   "id": "91635a6d38076667",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('聂茹凤', [ 90,  95,  97], 19) ('谭鑫宇', [ 89,  99,  69], 18)\n",
      " ('韩耀祖', [ 77, 100,  89], 16)]\n",
      "('聂茹凤', [90, 95, 97], 19)\n",
      "聂茹凤\n",
      "[90 95 97]\n",
      "[('f0', '<U3'), ('f1', '<i4', (3,)), ('f2', '<i4')]\n",
      "==============================\n",
      "[('聂茹凤', [ 90,  95,  97], 19) ('谭鑫宇', [ 89,  99,  69], 18)\n",
      " ('韩耀祖', [ 77, 100,  89], 16)]\n",
      "[('name', '<U3'), ('score', '<i4', (3,)), ('age', '<i4')]\n",
      "('聂茹凤', [90, 95, 97], 19)\n",
      "聂茹凤\n",
      "[90 95 97]\n",
      "==============================\n",
      "[('聂茹凤', [ 90,  95,  97], 19) ('谭鑫宇', [ 89,  99,  69], 18)\n",
      " ('韩耀祖', [ 77, 100,  89], 16)]\n",
      "[('name', '<U3'), ('score', '<i4', (3,)), ('age', '<i4')]\n",
      "('聂茹凤', [90, 95, 97], 19)\n",
      "聂茹凤\n",
      "[90 95 97]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T01:35:36.336818Z",
     "start_time": "2025-08-26T01:35:36.327310Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 日期类型\n",
    "f = np.array(['2025','2025-08','2025-08-26','2025-08-26 09:30:12'])\n",
    "f = f.astype('M8[D]') # 将字符串转换成日期类型\n",
    "f = f.astype('i4') # 把日期转换成int类型，就是距离计算机元年的天数\n",
    "print(f)\n",
    "print(f.dtype)"
   ],
   "id": "40427c7dea5b065e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20089 20301 20326 20326]\n",
      "int32\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-26T01:40:51.354690Z",
     "start_time": "2025-08-26T01:40:51.346861Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算聂茹凤到今天存在了多少天了\n",
    "birthday = np.array(['2004-05-22']).astype('M8[D]').astype('i4')\n",
    "now = np.array(['2025-08-26']).astype('M8[D]').astype('i4')\n",
    "\n",
    "print(f'出生{now-birthday}天了')\n"
   ],
   "id": "6b3939bca8557aec",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出生[7766]天了\n"
     ]
    }
   ],
   "execution_count": 53
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
